package model;
import java.util.*;


public abstract class PersonContainer
{protected ArrayList <Person> personCollection;
    
    private static PersonContainer instance;
    
    public PersonContainer()
    {
       personCollection=new ArrayList<Person>();
       
    }
    /*public static PersonContainer getInstance() {
        if(instance == null) {
            instance = new PersonContainer();
        }
        return instance;
    }*/
    public void addPerson(Person person){
        personCollection.add(person);
    }
    public void printPerson(){
    System.out.println("List of People: ");
    for(Person person:personCollection){
        System.out.println("" + person.getName());
    }
    }
    public void removePerson(String address){
        Person person=findPerson(address); 
        if(person !=null){
            boolean ok=personCollection.remove(person);
            if(ok){
                System.out.println("Person with the address " + address + " is now deleted");
                  } 
                }
            else{ System.out.println("There is no one living on " + address);
                }
            }
    public boolean updatePerson(String name, Person newPerson){
      boolean success=false;
    
        for(int i=0; i<personCollection.size();i++){ 
        if(personCollection.get(i).getName().equals(name)){
        personCollection.remove(i);
        personCollection.add(newPerson);
        success=true;
        }
       } 
      return success;
    }
    public Person findPerson(String paddress){
        Person person=null;
        int index=0;
        boolean found=false;
        while(index < personCollection.size() && !found){
            person= personCollection.get(index);
            if(person.getAddress().equals(paddress))
            {found = true;
            }
            else {
                index++;
            }
        }
        if(found) {
            return personCollection.get(index);
        }
        else {
            return null;
        }
    }
}
    

